package ru.ntcreb.csir.libs.methods.SVCH;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlBuilder {

	public XmlBuilder() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			builder = factory.newDocumentBuilder();
		}

		catch (ParserConfigurationException e) { // use logger
		}
	}

	private static DocumentBuilder builder;

	/**
	 * XML
	 * 
	 * @throws TransformerException
	 * @throws FileNotFoundException
	 */
	public static void writeParamXML(Double approximationCoefficient_K,
			Double approximationCoefficient_N, Double voltage_U0,
			Double stepness_S) throws FileNotFoundException,
			TransformerException {

		Document doc = builder.newDocument();
		Element RootElement = doc.createElement("data");

		Element KpodUKV = doc.createElement("approximationCoefficient_K");
		KpodUKV.setAttribute("val", approximationCoefficient_K.toString());
		RootElement.appendChild(KpodUKV);

		Element f1prd = doc.createElement("approximationCoefficient_N");
		f1prd.setAttribute("val", approximationCoefficient_N.toString());
		RootElement.appendChild(f1prd);

		Element f2prd = doc.createElement("voltage_U0");
		f2prd.setAttribute("val", voltage_U0.toString());
		RootElement.appendChild(f2prd);

		Element sFiN1 = doc.createElement("stepness_S");
		sFiN1.setAttribute("val", stepness_S.toString());
		RootElement.appendChild(sFiN1);

		doc.appendChild(RootElement);
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		transformer.transform(new DOMSource(doc), new StreamResult(
				new FileOutputStream("D:\\11\\tempXml.xml")));
	}

}
